package com.spring.boot.standard.admin.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import com.spring.boot.standard.admin.common.utils.GsonUtil;
import com.spring.boot.standard.admin.dao.WebpagesRepository;
import com.spring.boot.standard.admin.entity.Webpages;
import com.spring.boot.standard.admin.service.WebpagesService;

/**
 * @author qinhao
 * @since Tue Oct 12 16:24:50 CST 2021
 */
@Service
@EnableTransactionManagement
public class WebpagesServiceImpl implements WebpagesService {
	
    @Autowired
    private WebpagesRepository webpagesRepository;

    @SuppressWarnings("serial")
	@Override
	public Page<Webpages> findAll(Pageable pageable,Object[] args) {
		Page<Webpages> result = webpagesRepository.findAll(new Specification<Webpages>() {
	        @Override
	        public Predicate toPredicate(Root<Webpages> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				//List<Predicate> list = new ArrayList<Predicate>(); 多条件搜索的写法
				if (!StringUtils.isEmpty(args[0]) && !StringUtils.isEmpty(args[1])) {
					/**多条件搜索的写法
					list.add(cb.like(root.get("webpagesid").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("url").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("title").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("remark").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("picture").as(String.class), "%" + args[0] + "%"));
					list.add(cb.like(root.get("username").as(String.class), "%" + args[0] + "%"));
					Predicate[] p = new Predicate[list.size()];
					return cb.or(list.toArray(p));*/
					return cb.like(root.get(args[0].toString()).as(String.class),"%" + args[1] + "%");
				}else{
					return null;
				}
	        }
	    }, pageable);
	    return result;
	}

	@Override
	public Webpages findById(String webpagesid) {
		return webpagesRepository.findById(webpagesid).get();
	}

	@Override
	public void deleteById(String webpagesid) {
		webpagesRepository.deleteById(webpagesid);;
	}

	@Override
	public Webpages save(Webpages webpages) {
		return webpagesRepository.save(webpages);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public int saveFromList(List<Map<String,String>> list) throws Exception {
		int i=0;
		List<Webpages> webpageslist = new ArrayList<Webpages>();
		for(Map<String,String> map:list){
			Webpages webpages = GsonUtil.getGson().fromJson(GsonUtil.getGson().toJson(map),Webpages.class);
			webpageslist.add(webpages);
		}
		for(Webpages webpages:webpageslist){
			webpagesRepository.save(webpages);
			i++;
		}
		return i;
	}

    
}
